#ifndef TIMEMANAGER_H
#define TIMEMANAGER_H

#include <SDL.h>
#include "game.h"

#include <list>
#include <algorithm>

class Timer;

//This class does the time would be exactly the
//same in all computers. It has got the list
//of timers and update them too.
class TimeManager
{
	public:
		~TimeManager();
		
		static TimeManager& self();
		static bool exists();

		bool init(int atimetick);
		void update();
		void shutdown();

		//Add and erase timers
		void addTimer(Timer* t);
		void delTimer(Timer* t);

		//This function returns true if the program must
		//do a logic cicle.
		bool isLogictick();
		//The same but if must update the screen
		bool isGraphictick();

		//returns how many cicles the program does in a second
		unsigned ticksPerSecond()const{return 1000/timetick;}
		//returns how many miliseconds there are among ticks
		float getTimeTick()const{return (float)timetick/1000;}
		void reset(){logic_tick=tick;}

	protected:
		TimeManager();

		static TimeManager* m_self;

		//Backcall function executed in other thread and increments
		//the tick.
		Uint32 static tickCount(Uint32 interval,void *parameter);

		//The actual tick
		static volatile unsigned long tick;
		unsigned long time;
		//timetick
		unsigned timetick;
		//frameskip consecutives and maximum frameskip consecutive
		unsigned frameskip,max_frameskip;
		//how many frameskip in a second
		unsigned totalframeskip;
		unsigned int max_fps;
		//Frames per second
		unsigned int graphic_tick;
		//Tics per second
		//Old_tick used to know when is a second
		unsigned long logic_tick,old_tick;
		SDL_TimerID timerID;

		std::list<Timer*> to_add;
		std::list<Timer*> timers;
		std::list<Timer*> to_del;
};

#endif
